const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    open_id: '',
    runData: [],
    phone: '',
    modalName: '',
    sessionKey: '',
    declare: '',
    prize: '',
    hdgz: '',
    shareText: '我参加了网云小镇全民线上马拉松，你也来快来参加吧！',
    isLoad: false
  },
  onLoad() {
    wx.showShareMenu({
      withShareTicket: true
    })

    const sessionKey = wx.getStorageSync('sessionKey');
    const open_id = wx.getStorageSync('user_new');
    const phone = wx.getStorageSync('phone');
    const declare = wx.getStorageSync('declare');

    if (declare) {
      this.setData({
        declare: declare,
      })
    }

    this.setData({
      open_id: open_id,
      phone: phone,
      sessionKey: sessionKey,
    })

    if (sessionKey == undefined || sessionKey == '') {
      this.setData({
        open_id: '',
      })

      wx.setStorageSync('user_new', '')
    }

    this.getRunDdata()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: this.data.shareText,
      path: '/pages/runData/index'
    }
  },

  onPullDownRefresh: function() {
    const that = this

    this.setData({
      isLoad: true
    })
    this.getRunDdata()
    setTimeout(() => {
      wx.showToast({
        title: '刷新成功'
      })

      that.setData({
        isLoad: false
      })

      wx.stopPullDownRefresh()
    }, 1000)

    return true;
  },

  runData: function() {
    const that = this
    wx.getWeRunData({
      success (res) {

        const encryptedData = res.encryptedData
        that.runDataSign({encrypted_data: encryptedData, iv: res.iv, open_id: that.data.open_id, declare: that.data.declare})
      },
      fail (e) {
        wx.showToast({
          title: '获取微信运动失败',
        })
      }
    })
  },

  runDataSign(data) {
    const that = this
    app.core('post', 'user/runDataSign', data, (res) => {
      if (res.resultCode == 200) {
        that.setData({
          runData: res.data
        })

        wx.showToast({
          title: '签到成功！',
        })

        wx.setStorageSync('declare', that.data.declare);
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  getRunDdata() {
    const that = this
    app.core('get', 'user/runData', {}, (res) => {
      if (res.resultCode == 200) {
        that.setData({
          runData: res.data,
          prize: res.prize,
          hdgz: res.hdgz,
        })

        if (res.shareText != '') {
          that.setData({
            shareText: res.shareText
          })
        }

        if (res.declare != '' && that.data.declare == '') {
          that.setData({
            declare: res.declare
          })
        }
      }
    })
  },

  handleContent: function () {
    wx.previewImage({
      current: this.data.hdgz,
      urls: [
        this.data.hdgz,
      ]
    })
  },

  handlePrize: function () {
    wx.previewImage({
      current: this.data.prize,
      urls: [
        this.data.prize,
      ]
    })
  },

  getPhoneNumber (e) {
    const that = this
    if (e.detail.iv && e.detail.encryptedData) {
      app.core('post', 'user/getPhoneNumber', {encrypted_data: e.detail.encryptedData, iv: e.detail.iv, open_id: this.data.open_id}, (res) => {
        if (res.resultCode == 200) {
          if (res.data.purePhoneNumber) {
            wx.setStorageSync("phone", res.data.purePhoneNumber)
            that.setData({
              phone: res.data.purePhoneNumber
            })
          } else {
            wx.showToast({
              title: '获取手机号失败',
            })
          }
        } else {
          wx.showToast({
            title: '获取手机号失败',
          })
        }
      })
    } else {
      wx.showToast({
        title: '获取手机号失败',
      })
    }
  },

  toLogin: function () {
    wx.reLaunch({
      url: '/pages/member/index?redirect=' + encodeURIComponent('/pages/runData/index')
    })

    return false
  },

  hideModal() {
    this.setData({
      modalName: ''
    })
  },

  showModal() {
    this.setData({
      modalName: 'show'
    })
  },

  handleModal() {
    if (this.data.declare == '') {

      wx.showToast({
        title: '参赛宣言不能为空',
      })

      return
    }

    this.runData()
    this.hideModal()
  },

  handleInput(e) {
    if (e.detail.value) {
      if (e.detail.value != '' && e.detail.value.length > 50) {
        wx.showToast({
          title: '参赛宣言太长啦！',
        })

        e.detail.value = e.detail.value.substr(0, 50)
      }

      this.setData({
        declare: e.detail.value
      })
    }
  }
})
